<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\AdvType;
use App\Models\SystemRegion;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;

class AdvTypeController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new AdvType(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('adv_type_name');
            $grid->column('adv_img', '图片')->image('', 60, 60);
            $grid->column('category_id')->display(function ($category_id) {
                if ($category_id) {
                    $ategory = \App\Models\ArticleCategory::find($category_id);
                    return $ategory ? $ategory->category_name : '';
                } else {
                    return '其他';
                }
            });
            $grid->column('region_id')->display(function ($region_id) {
                if ($region_id) {
                    $region = \App\Models\SystemRegion::find($region_id);
                    return $region ? $region->region_name : '';
                } else {
                    return '';
                }
            });
            $grid->column('created_at');
//            $grid->column('updated_at')->sortable();

            $grid->disableViewButton(); // 禁用详情按钮
            $grid->quickSearch(['adv_type_name'])->placeholder('搜索...');
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new AdvType(), function (Show $show) {
            $show->field('id');
            $show->field('adv_type_name');
            $show->field('category_id');
            $show->field('region_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new AdvType(), function (Form $form) {
            $form->display('id');
            $form->text('adv_type_name')->required();
            $form->image('adv_img', '图片')->move('adv/'.date('Y-m-d'))->autoUpload();
            $category = \App\Admin\Repositories\ArticleCategory::getList(0, 3);
            $category[0] = '其他';
            $needRegionCategory = \App\Admin\Repositories\ArticleCategory::needRegionIDCategory();

            $form->select('category_id', '所属分类')
//                ->when($needRegionCategory, function (Form $form) {
//                    $region = \App\Admin\Repositories\SystemRegion::getList();
//                    $form->select('region_id', '所属地区')->options($region)->default(0, true);
//                })
                ->options($category)
//                ->required()
                ->default(0, true);

            $form->radio('is_region', '是否限制地区')->options([1 => '是', 0 => '否'])->default(0);
            $form->display('created_at');
            $form->display('updated_at');

            $form->submitted(function ($form) {
                $form->region_id = empty($form->region_id) ? 0 : $form->region_id;
            });
        });
    }
}
